import commonJS from "@/utils/common.js"
import Http from "@/utils/api.js"
export default {
	data() {
		return {
			timer: null,
			authCodeTime: 59,
			authCode: '',
			isAuthCode: false,
		}
	},
	// 销毁定时器
	beforeDestroy() {
		if (this.timer) {
			clearInterval(this.timer);
			this.timer = null;
		}
	},
	onUnload() {
		if (this.timer) {
			clearInterval(this.timer);
			this.timer = null;
		}
	},
	methods: {


		// 发送验证码
		handleSendPhone() {
			if (this.phoneNumber === '') {
				uni.showToast({
					title: '请输入手机号码',
					icon: 'none',
				})
				return;
			}
			if (commonJS.isPhone(this.phoneNumber) === false) {
				uni.showToast({
					title: '请输入正确手机格式',
					icon: 'none',
				})
				return;
			}
			let params = {
				mobile: this.phoneNumber
			}
			Http.sendPhoneMsg(params, true).then(res => {
				if (res.returnCode === '6006') {
					uni.showToast({
						title: '验证码发送成功',
						icon: 'success',
					})
					this.isAuthCode = true;
					this.handleAuthCodeTime();
				} else {
					uni.showToast({
						title: res.returnMessage,
						icon: 'none',
					})
				}
			})
		},
		// 定时器
		handleAuthCodeTime() {
			let _this = this;
			let time = 59;
			this.timer = setInterval(function() {
				if (time === 1) {
					clearInterval(_this.timer);
					_this.isAuthCode = false;
				}
				time--;
				_this.authCodeTime = time;
			}, 1000)
		},
	}
}
